package com.gu.controller;

import com.github.pagehelper.PageInfo;
import com.gu.VO.LayuiVO;
import com.gu.VO.ServerResponse;
import com.gu.dao.SettingDAO;
import com.gu.entity.Setting;
import com.gu.entity.VehicleInfo;
import com.gu.service.SettingService;
import com.gu.service.VehicleInfoService;
import com.gu.service.VehicleRefuelingInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author: gusongliao
 * @Description: 车辆加油控制类
 * @createDate 2023/01/05 19:32
 */
@Controller
@RequestMapping("/vehicleRefuelingInfo")
public class VehicleRefuelingInfoController {

    @Autowired
    private VehicleRefuelingInfoService vehicleRefuelingInfoService;

    @Autowired
    private VehicleInfoService vehicleInfoService;

    @PostMapping("/upload")
    @ResponseBody
    public ServerResponse upload(MultipartFile[] file) throws IOException {
        return vehicleRefuelingInfoService.importFile(file);
    }

    @RequestMapping("/vehicleRefuelingInfoList")
    @ResponseBody
    public LayuiVO userList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "15") Integer pageSize, String vehicleSelectorParam
                            ,String submitTimeStart
                            ,String submitTimeEnd
                            ,String createTimeStart
                            ,String createTimeEnd
                            ,Integer matchRule
    ) throws UnsupportedEncodingException {
        if (vehicleSelectorParam == null) {
            vehicleSelectorParam = "";
        }
        if (submitTimeEnd != null && !"".equals(submitTimeEnd)) {
            submitTimeEnd += " 23:59:59";
        }
        if (createTimeEnd != null && !"".equals(createTimeEnd)) {
            createTimeEnd += " 23:59:59";
        }
        PageInfo userList = vehicleRefuelingInfoService.getAllVehicleRefuelingInfo(pageNum,pageSize,new String(vehicleSelectorParam.getBytes(StandardCharsets.ISO_8859_1),"utf-8"),submitTimeStart,submitTimeEnd,createTimeStart,createTimeEnd,matchRule);
        return LayuiVO.success(userList.getTotal(),userList.getList());
    }

    @RequestMapping("/exportFileList")
    @ResponseBody
    public ServerResponse userList(String vehicleSelectorParam
            ,String submitTimeStart
            ,String submitTimeEnd
            ,String createTimeStart
            ,String createTimeEnd
            ,Integer matchRule
    ) throws UnsupportedEncodingException {
        if (vehicleSelectorParam == null) {
            vehicleSelectorParam = "";
        }
        if (submitTimeEnd != null && !"".equals(submitTimeEnd)) {
            submitTimeEnd += " 23:59:59";
        }
        if (createTimeEnd != null && !"".equals(createTimeEnd)) {
            createTimeEnd += " 23:59:59";
        }
        return vehicleRefuelingInfoService.exportFile(vehicleSelectorParam, submitTimeStart, submitTimeEnd, createTimeStart, createTimeEnd,matchRule);
    }


    @RequestMapping("/getAllVehicleInfo")
    @ResponseBody
    public List<VehicleInfo> getAllVehicleInfo() {
        return vehicleInfoService.getAllVehicleInfo();
    }

    @PostMapping("/delete")
    @ResponseBody
    public ServerResponse delete(String dataIds) throws IOException {
        return vehicleRefuelingInfoService.delete(dataIds);
    }

}
